<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="仓库编号" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="仓库地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入仓库地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:warehouse:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:warehouse:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:warehouse:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:warehouse:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-folder-add"
          size="mini"
          :disabled="multiple"
          @click="handleMerge"
          v-hasPermi="['system:warehouse:export']"
          >合并</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="warehouseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="仓库代码"
        align="center"
        prop="warehouseCode"
        width="120"
      />
      <el-table-column
        label="仓库名称"
        align="center"
        prop="warehouseName"
        width="120"
      />
      <el-table-column label="所属机构" align="center" prop="mechanismId">
        <template slot-scope="scope">
          <span
            v-if="scope.row.mechanismId == item.id"
            v-for="item in mechanismList"
            :key="item.id"
          >
            {{ item.mechanismName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="所属城市" align="center" prop="city" />
      <el-table-column label="仓库地址" align="center" prop="address" />
      <el-table-column
        label="仓库类型"
        align="center"
        prop="warehouseType"
        width="80"
      >
        <template slot-scope="scope">
          <div
            :style="
              scope.row.warehouseType == '2' ? 'display: none' : 'display:block'
            "
          >
            <el-tag type="success" disable-transitions>自有 </el-tag>
          </div>
          <div
            :style="
              scope.row.warehouseType == '1' ? 'display: none' : 'display:block'
            "
          >
            <el-tag type="primary" disable-transitions>租赁 </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" prop="tel" width="120" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:warehouse:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:warehouse:remove']"
          >删除</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="look(scope.row.warehouseCode)"
          >批货清单</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="lookgoods(scope.row.warehouseCode)"
          >商品信息管理</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改#仓库信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库代码" prop="warehouseCode">
          <el-input
            :disabled="true"
            v-model="form.warehouseCode"
            placeholder="选择城市后生成仓库代码"
          />
        </el-form-item>
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>
        <el-form-item label="所属机构" prop="mechanismId">
          <el-select
            v-model="form.mechanismId"
            placeholder="请选择下拉选择所属机构"
            clearable
            :style="{ width: '100%' }"
          >
            <el-option
              v-for="(item, index) in mechanismList"
              :key="index"
              :label="item.mechanismName"
              :value="item.id"
              :disabled="item.disabled"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="仓库类型" prop="warehouseType">
          <el-radio-group v-model="form.warehouseType" size="medium">
            <el-radio
              v-for="(item, index) in warehouseTypeOptions"
              :key="index"
              :label="item.value"
              :disabled="item.disabled"
              >{{ item.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>
        <el-form-item label="配送范围" prop="deliveryRadius">
          <el-input v-model="form.deliveryRadius" placeholder="请输入配送范围"
            ><template slot="append">m</template></el-input
          >
        </el-form-item>
        <el-form-item label="所在城市" prop="city">
          <el-cascader
            ref="cascader"
            :options="areaSelectData"
            @change="handleChange"
            class="full-width"
            size="large"
            v-model="selectedOptions"
            placeholder="请选择您所在的城市"
            :disabled="form.id != null"
          />
        </el-form-item>
        <el-form-item label="作息时间">
          <template>
            <el-time-picker
              v-model="form.startTime"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59',
              }"
              placeholder="开始营业时间"
            >
            </el-time-picker>
            <el-time-picker
              v-model="form.endTime"
              :picker-options="{
                selectableRange: '00:00:00 - 23:59:59',
              }"
              placeholder="结束营业时间"
            >
            </el-time-picker>
          </template>
        </el-form-item>
        <el-form-item label="物流费用" prop="deliveryfeeAverykm">
          <el-input v-model="form.deliveryfeeAverykm" placeholder="请输入物流费"
            ><template slot="append">元/公里</template></el-input
          >
        </el-form-item>
        <el-form-item label="起送数量" prop="startSendNum">
          <el-input v-model="form.startSendNum" placeholder="请输入起送数量">
            <template slot="append">件</template>
          </el-input>
        </el-form-item>
        <template>
          <div>
            <baiduMap @map-baidu="getMapCode"> </baiduMap>
          </div>
        </template>
        <el-form-item label="经纬度" prop="mapCode">
          <el-input
            :disabled="true"
            v-model="form.mapCode"
            placeholder="选择仓库地址后自动获取"
          />
        </el-form-item>
        <el-form-item label="仓库地址" prop="address">
          <el-input
            :disabled="true"
            v-model="form.address"
            placeholder="选择仓库地址后自动获取"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  listWarehouse,
  getWarehouse,
  delWarehouse,
  addWarehouse,
  updateWarehouse,
  selectMaxId,
  mergeWarehouse,
} from "@/api/system/warehouse";
import { listMechanism } from "@/api/system/mechanism";
import baiduMap from "@/components/baiduMap";
import Vue from "vue";
import BaiduMap from "vue-baidu-map";
Vue.use(BaiduMap, {
  ak: "Phqhd1m9VR4giYknwRswR3jRln9qpuSq",
});
import { regionData, codeToText } from "element-china-area-data";

export default {
  name: "Warehouse",
  components: { baiduMap },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // #仓库信息表格数据
      warehouseList: [],
      // 机构信息表格数据
      mechanismList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseCode: null,
        mechanismId: null,
        warehouseType: null,
        address: null,
        deliveryfeeAverykm: null,
        startTime: null,
        endTime: null,
        deliveryRadius: null,
        mapCode: null,
        tel: null,
        startSendNum: null,
        isDeleted: null,
      },
      // 表单参数
      form: {},
      //仓库类型
      warehouseTypeOptions: [
        { label: "自用", value: 1 },
        { label: "租赁", value: 2 },
      ],
      // 表单校验
      rules: {
        mechanismId: [
          { required: true, message: "机构id不能为空", trigger: "blur" },
        ],
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
        ],
        city: [
          { required: true, message: "所在城市不能为空", trigger: "blur" },
        ],
      },
      areaSelectData: regionData, //省市区三级联动数据
      selectedOptions: [],
    };
  },
  created() {
    this.getList();
    this.getMechanismList();
  },
  methods: {

    look(warehouseCode){
      this.$router.push("/ruoyi-goods/batchList/list/"+warehouseCode);
    },

    lookgoods(warehouseCode){
      this.$router.push("/ruoyi-goods/batchList/listgoods/"+warehouseCode);
    },

    /** 查询#仓库信息列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then((response) => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询机构信息列表 */
    getMechanismList() {
      this.loading = true;
      listMechanism(null).then((response) => {
        this.mechanismList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        warehouseCode: null,
        mechanismId: null,
        warehouseType: null,
        address: null,
        city: null,
        deliveryfeeAverykm: null,
        startTime: null,
        endTime: null,
        deliveryRadius: null,
        mapCode: null,
        tel: null,
        startSendNum: null,
        isDeleted: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.selectedOptions = [];
      this.open = true;
      this.title = "添加仓库信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWarehouse(id).then((response) => {
        this.form = response.data;
        this.selectedOptions = response.data.cityCode;
        this.open = true;
        this.title = "修改仓库信息";
      });
    },
    /**省市区三级联动 */
    handleChange(value) {
      //value为省市区code数组
      if (value) {
        selectMaxId().then((maxId) => {
          this.form.warehouseCode =
            "W" + value[2] + String(maxId + 1).padStart(4, "0");
        });
        this.form.city =
          codeToText[value[0]] +
          "-" +
          codeToText[value[1]] +
          "-" +
          codeToText[value[2]];
      }
    },
    /** 获取经纬度 */
    getMapCode(center) {
      this.form.mapCode = center.latitude + "," + center.longitude;
      this.form.address = center.searchJingwei;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWarehouse(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除#仓库信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delWarehouse(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/warehouse/export",
        {
          ...this.queryParams,
        },
        `warehouse_${new Date().getTime()}.xlsx`
      );
    },
    /** 合并按钮操作 */
    handleMerge() {
      this.$confirm(
        '是否确认合并仓库信息编号为"' + this.ids + '"的数据项？',
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(async () => {
          let response = await mergeWarehouse(this.ids);
          this.$message(response.msg); //提示消息
          await this.getList(); //重新加载数据
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消合并",
          });
        });
    },
  },
};
</script>
